﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ResxCrunch.Utils;

namespace ResxCrunch
{
    /// <summary>
    /// Select language to remove form class
    /// </summary>
    public partial class FrmSelectLang2Remove : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmSelectLang2Remove"/> class.
        /// </summary>
        public FrmSelectLang2Remove()
        {
            InitializeComponent();
        }

        #region Properties
        /// <summary>
        /// Gets or sets the table containig the columns with the languages.
        /// </summary>
        /// <value>Table containig the columns with the languages</value>
        public DataTable DataSource
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the selected Languages
        /// </summary>
        /// <value>List containing the selected Languages.</value>
        public List<string> SelectedLanguages
        {
            get;
            set;
        }

        /// <summary>
        /// Sets a value indicating whether this <see cref="FrmMain"/> is busy.
        /// </summary>
        /// <value><c>true</c> if busy; otherwise, <c>false</c>.</value>
        public bool Busy
        {
            set
            {
                if (value)
                {
                    lblBusy.Visible = true;
                    this.Refresh();
                }
                else
                {
                    lblBusy.Visible = false;
                }
            }
        }
        #endregion Properties

        #region Private Methods
        /// <summary>
        /// Updates the values.
        /// </summary>
        private void UpdateValues()
        {
            lblNrSelected.Text = string.Concat(lstLanguages.SelectedItems.Count, "/", lstLanguages.Items.Count, " selected");
        }

        /// <summary>
        /// Extracts from the source table and loads the languages
        /// </summary>
        private void LoadLanguages()
        {
            this.Busy = true;

            List<string> cols = new List<string>();
            foreach (DataColumn dc in this.DataSource.Columns)
            {
                if (!dc.ColumnName.Equals(Enums.DataColumn.Nr, StringComparison.OrdinalIgnoreCase) &&
                    !dc.ColumnName.Equals(Enums.DataColumn.Directory, StringComparison.OrdinalIgnoreCase) &&
                    !dc.ColumnName.Equals(Enums.DataColumn.File, StringComparison.OrdinalIgnoreCase) &&
                    !dc.ColumnName.Equals(Enums.DataColumn.Key, StringComparison.OrdinalIgnoreCase) &&
                    !dc.ColumnName.Equals(Enums.DataColumn.DefaultValue, StringComparison.OrdinalIgnoreCase))
                {
                    cols.Add(dc.ColumnName);
                }
            }

            cols.Sort();

            lstLanguages.Items.AddRange(cols.ToArray());

            this.Busy = false;
        }
        #endregion Private Methods

        #region Events
        /// <summary>
        /// Handles the Load event of the FrmSelectLang2Remove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectLang2Remove_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Shown event of the FrmSelectLang2Remove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectLang2Remove_Shown(object sender, EventArgs e)
        {
            this.LoadLanguages();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the SelectedValueChanged event of the lstLanguages control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lstLanguages_SelectedValueChanged(object sender, EventArgs e)
        {
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            List<string> selected = new List<string>();

            foreach (string item in lstLanguages.SelectedItems)
            {
                selected.Add(item.ToString());
            }

            this.SelectedLanguages = selected;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        #endregion Events
    }
}
